<template>
    <van-nav-bar title="编辑地址" left-text="" left-arrow @click-left="goBack" />
    <van-address-edit :area-list="areaList" show-set-default :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :address-info="info" show-delete @save="onSave" @delete="onDelete" />

</template>
<script setup>
import { areaList } from '@vant/area-data';
const { proxy } = getCurrentInstance()
const LoginUser = reactive(proxy.$cookies.get('LoginUser'));
const goBack = () => {
    proxy.$router.go(-1)
}
const addressid = ref(proxy.$route.query.addressid ?? 0)
const info = ref({})
onBeforeMount(() => {
    getAddressInfoData()
})
const getAddressInfoData = async () => {
    let result = await Api.AddressInfo({
        busid: LoginUser.id,
        addressid: addressid.value
    })

    if (result.code == 1) {
        info.value = {
            name: result.data.consignee,
            tel: result.data.mobile,
            areaCode: result.data.district,
            addressDetail: result.data.address,
            isDefault: result.data.status == '1' ? true : false
        }
    }
}
const onSave = async (val) => {
    let data = {
        busid: LoginUser.id,
        consignee: val.name,
        code: val.areaCode,
        address: val.addressDetail,
        mobile: val.tel,
        status: val.isDefault ? '1' : '0',
        addressid: addressid.value
    }
    let result = await Api.AddressEdit(data)
    if (result.code == 1) {
        success({
            message: result.msg,
            onClose: () => {
                goBack()
            }
        });
    } else {
        error(result.msg);
    }
};

const onDelete = (val) => {
    Confirmdialog({
        title: '删除',
        message:
            '确认是否删除收货地址?',
    })
        .then(async () => {
            let result = await Api.AddressDel({
                busid: LoginUser.id,
                addressid: addressid.value
            })
            if (result.code == 1) {
                success({
                    message: result.msg,
                    onClose: () => {
                        goBack()
                    }
                });
            } else {
                error(result.msg);
            }
        })
        .catch(() => { });
}
</script>